<?php
	require_once('/applications/recaptchalib.php');

	class com_contact extends component
	{
		private $name_error = 0;
		private $email_error = 0;
		private $message_error = 0;
		private $captcha_error = 0;

		public function get_content()
		{
?>
			<div id="contact-wrapper">
<?php
			if (isset($_POST['name'])) {
				$name = $_POST['name'];
				$email = $_POST['email'];
				$title = $_POST['title'];
				$captcha = $_POST['recaptcha_response_field'];
				$captcha_challenge = $_POST['recaptcha_challenge_field'];
				$message = $_POST['message'];
				$check = $this->check_contact_data($name,$email,$captcha,$captcha_challenge,$message);
				if ($check == 0)
					$this->send_mail($name,$email,$title,$message);
			}
?>
			
				<h1>Contact</h1>
				<form action="index.php?option=com_contact" method="post" name="contact-form" id="contact-form" onsubmit="return isValidForm();">
					<label for="name" id="lbl-name">Your name:</label>
					<?php if ($this->name_error == 1) echo '<label class="error-mess"> (This field is required!)</label>'?>
					<input type="text" name="name" id="name" />
					<label for="email" id="lbl-email">Email:</label>
					<?php if ($this->email_error == 1) echo '<label class="error-mess"> (This field is required!)</label>'?>
					<?php if ($this->email_error == 2) echo '<label class="error-mess"> (The email is invalid!)</label>'?>
					<input type="text" name="email" id="email" />
					<label for="title" id="lbl-message">Title:</label>
					<input type="text" name="title" id="title" />
					<label for="message">Message:</label>
					<?php if ($this->message_error == 1) echo '<label class="error-mess"> (This field is required!)</label>'?>
					<textarea name="message" id="message"></textarea>
					<label for="message">Captcha:</label>
					<?php if ($this->captcha_error == 1) echo '<label class="error-mess"> (This field is required!)</label>'?>
					<?php if ($this->captcha_error == 2) echo '<label class="error-mess"> (The captcha is not match!)</label>'?>
					<?php
						$publickey = "6LekfN8SAAAAAIAGxqcy7yIEek8Vm9xN83EuyVxV"; 
						echo recaptcha_get_html($publickey);
					?>
					<input type="submit" class="button" id="submit" value="Send" />
					<input type="button" class="button" id="clear" value="Clear" onclick="clear_info();" />
				</form>
			</div><!-- End #contact-wrapper -->
<?php
		}
		//Create breadcrumbs
		private function create_breadcrumbs()
		{
?>
			<div class="breadcrumbs" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb">
				<a class="first" href="http://localhost/cms" itemprop="url"><span itemprop="title">Home</span></a>
				<a href="http://localhost/cms/index.php?option=com_contact" itemprop="url"><span itemprop="title">Search</span></a>
			</div><!-- End .breadcrumbs-->
<?php
		}
		//Check the contact data
		private function check_contact_data($name,$email,$captcha,$captcha_challenge,$message) {
			$error = 0;
			if ($name == '') {
				$this->name_error = 1;
				$error = 1;
			}
			if ($email == '') {
				$this->email_error = 1;
				$error = 1;
			}
			else {
				$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
				if (!preg_match($regex, $email)) {
					$this->email_error = 2;
					$error = 1;
				}
			}
			if ($captcha == '') {
				$this->captcha_error = 1;
				$error = 1;
			}
			else {
				$privatekey = "6LekfN8SAAAAAHoLLbwBH4QhiN-VLy_1Hh9rsBFv";
			 	$resp = recaptcha_check_answer ($privatekey,
			                            	$_SERVER["REMOTE_ADDR"],
			                            	$captcha_challenge,
			                            	$captcha);
			 	if (!$resp->is_valid) {
				 	$this->captcha_error = 2;
					$error = 1;
				}
			}
			if ($message == '') {
				$this->message_error = 1;
				$error = 1;
			}
			if ($error == 1)
				echo("<div class='message error'>The information is not valid.</div>");
			return $error;
		}
		//Send mail
		private function send_mail($name,$email,$title,$message)
		{
			require_once "mail.php";

	        $from = $email;
	        $to = "auduongphong_87@yahoo.com";
	        $subject = $title;
	        $body = $name." sent you a message:\n";
	        $body .= $message;

	        $host = "ssl://smtp.gmail.com";
	        $port = "465";
	        $username = "hotro.khoatn@gmail.com";  //<> give errors
	        $password = "tarakemi";

	        $headers = array ('From' => $from,
	          'To' => $to,
	          'Subject' => $subject);
	        $smtp = Mail::factory('smtp',
	          array ('host' => $host,
	            'port' => $port,
	            'auth' => true,
	            'username' => $username,
	            'password' => $password));

	        $mail = $smtp->send($to, $headers, $body);

	        if (PEAR::isError($mail)) {
	          echo("<div id='error-message' class='message error'>Can't not sent your message. Please try again later.</div>");
	         } 
	         else {
	          echo("<div id='error-message' class='message success'>Your message has just been sent. Thanks for your contact!</div>");
	         }
		}
	}
?>